{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n*********************************\nTypesetting With XeLaTeX/LuaLaTeX\n*********************************\n\nHow to typeset text with the ``pgf`` backend in Matplotlib.\n\nUsing the ``pgf`` backend, matplotlib can export figures as pgf drawing commands\nthat can be processed with pdflatex, xelatex or lualatex. XeLaTeX and LuaLaTeX\nhave full unicode support and can use any font that is installed in the operating\nsystem, making use of advanced typographic features of OpenType, AAT and\nGraphite. Pgf pictures created by ``plt.savefig('figure.pgf')`` can be\nembedded as raw commands in LaTeX documents. Figures can also be directly\ncompiled and saved to PDF with ``plt.savefig('figure.pdf')`` by either\nswitching to the backend\n\n.. code-block:: python\n\n    matplotlib.use('pgf')\n\nor registering it for handling pdf output\n\n.. code-block:: python\n\n    from matplotlib.backends.backend_pgf import FigureCanvasPgf\n    matplotlib.backend_bases.register_backend('pdf', FigureCanvasPgf)\n\nThe second method allows you to keep using regular interactive backends and to\nsave xelatex, lualatex or pdflatex compiled PDF files from the graphical user interface.\n\nMatplotlib's pgf support requires a recent LaTeX_ installation that includes\nthe TikZ/PGF packages (such as TeXLive_), preferably with XeLaTeX or LuaLaTeX\ninstalled. If either pdftocairo or ghostscript is present on your system,\nfigures can optionally be saved to PNG images as well. The executables\nfor all applications must be located on your :envvar:`PATH`.\n\nRc parameters that control the behavior of the pgf backend:\n\n    =================  =====================================================\n    Parameter          Documentation\n    =================  =====================================================\n    pgf.preamble       Lines to be included in the LaTeX preamble\n    pgf.rcfonts        Setup fonts from rc params using the fontspec package\n    pgf.texsystem      Either \"xelatex\" (default), \"lualatex\" or \"pdflatex\"\n    =================  =====================================================\n\n<div class=\"alert alert-info\"><h4>Note</h4><p>TeX defines a set of special characters, such as::\n\n     # $ % & ~ _ ^ \\ { }\n\n   Generally, these characters must be escaped correctly. For convenience,\n   some characters (_,^,%) are automatically escaped outside of math\n   environments.</p></div>\n\n\n\nMulti-Page PDF Files\n====================\n\nThe pgf backend also supports multipage pdf files using ``PdfPages``\n\n.. code-block:: python\n\n    from matplotlib.backends.backend_pgf import PdfPages\n    import matplotlib.pyplot as plt\n\n    with PdfPages('multipage.pdf', metadata={'author': 'Me'}) as pdf:\n\n        fig1, ax1 = plt.subplots()\n        ax1.plot([1, 5, 3])\n        pdf.savefig(fig1)\n\n        fig2, ax2 = plt.subplots()\n        ax2.plot([1, 5, 3])\n        pdf.savefig(fig2)\n\n\nFont specification\n==================\n\nThe fonts used for obtaining the size of text elements or when compiling\nfigures to PDF are usually defined in the matplotlib rc parameters. You can\nalso use the LaTeX default Computer Modern fonts by clearing the lists for\n``font.serif``, ``font.sans-serif`` or ``font.monospace``. Please note that\nthe glyph coverage of these fonts is very limited. If you want to keep the\nComputer Modern font face but require extended unicode support, consider\ninstalling the `Computer Modern Unicode <https://sourceforge.net/projects/cm-unicode/>`_\nfonts *CMU Serif*, *CMU Sans Serif*, etc.\n\nWhen saving to ``.pgf``, the font configuration matplotlib used for the\nlayout of the figure is included in the header of the text file.\n\n.. literalinclude:: ../../gallery/userdemo/pgf_fonts.py\n   :end-before: plt.savefig\n\n\n\nCustom preamble\n===============\n\nFull customization is possible by adding your own commands to the preamble.\nUse the ``pgf.preamble`` parameter if you want to configure the math fonts,\nusing ``unicode-math`` for example, or for loading additional packages. Also,\nif you want to do the font configuration yourself instead of using the fonts\nspecified in the rc parameters, make sure to disable ``pgf.rcfonts``.\n\n.. only:: html\n\n    .. literalinclude:: ../../gallery/userdemo/pgf_preamble_sgskip.py\n        :end-before: plt.savefig\n\n.. only:: latex\n\n    .. literalinclude:: ../../gallery/userdemo/pgf_preamble_sgskip.py\n        :end-before: import matplotlib.pyplot as plt\n\n\n\nChoosing the TeX system\n=======================\n\nThe TeX system to be used by matplotlib is chosen by the ``pgf.texsystem``\nparameter. Possible values are ``'xelatex'`` (default), ``'lualatex'`` and\n``'pdflatex'``. Please note that when selecting pdflatex the fonts and\nunicode handling must be configured in the preamble.\n\n.. literalinclude:: ../../gallery/userdemo/pgf_texsystem.py\n   :end-before: plt.savefig\n\n\n\nTroubleshooting\n===============\n\n* Please note that the TeX packages found in some Linux distributions and\n  MiKTeX installations are dramatically outdated. Make sure to update your\n  package catalog and upgrade or install a recent TeX distribution.\n\n* On Windows, the :envvar:`PATH` environment variable may need to be modified\n  to include the directories containing the latex, dvipng and ghostscript\n  executables. See `environment-variables` and\n  `setting-windows-environment-variables` for details.\n\n* A limitation on Windows causes the backend to keep file handles that have\n  been opened by your application open. As a result, it may not be possible\n  to delete the corresponding files until the application closes (see\n  `#1324 <https://github.com/matplotlib/matplotlib/issues/1324>`_).\n\n* Sometimes the font rendering in figures that are saved to png images is\n  very bad. This happens when the pdftocairo tool is not available and\n  ghostscript is used for the pdf to png conversion.\n\n* Make sure what you are trying to do is possible in a LaTeX document,\n  that your LaTeX syntax is valid and that you are using raw strings\n  if necessary to avoid unintended escape sequences.\n\n* The ``pgf.preamble`` rc setting provides lots of flexibility, and lots of\n  ways to cause problems. When experiencing problems, try to minimalize or\n  disable the custom preamble.\n\n* Configuring an ``unicode-math`` environment can be a bit tricky. The\n  TeXLive distribution for example provides a set of math fonts which are\n  usually not installed system-wide. XeTeX, unlike LuaLatex, cannot find\n  these fonts by their name, which is why you might have to specify\n  ``\\setmathfont{xits-math.otf}`` instead of ``\\setmathfont{XITS Math}`` or\n  alternatively make the fonts available to your OS. See this\n  `tex.stackexchange.com question <http://tex.stackexchange.com/questions/43642>`_\n  for more details.\n\n* If the font configuration used by matplotlib differs from the font setting\n  in yout LaTeX document, the alignment of text elements in imported figures\n  may be off. Check the header of your ``.pgf`` file if you are unsure about\n  the fonts matplotlib used for the layout.\n\n* Vector images and hence ``.pgf`` files can become bloated if there are a lot\n  of objects in the graph. This can be the case for image processing or very\n  big scatter graphs.  In an extreme case this can cause TeX to run out of\n  memory: \"TeX capacity exceeded, sorry\"  You can configure latex to increase\n  the amount of memory available to generate the ``.pdf`` image as discussed on\n  `tex.stackexchange.com <http://tex.stackexchange.com/questions/7953>`_.\n  Another way would be to \"rasterize\" parts of the graph causing problems\n  using either the ``rasterized=True`` keyword, or ``.set_rasterized(True)`` as per\n  :doc:`this example </gallery/misc/rasterization_demo>`.\n\n* If you still need help, please see `reporting-problems`\n\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
